package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.system.domain.SysRole;
import com.pearadmin.system.domain.SysUser;
import com.pearadmin.system.domain.SysUserRole;
import com.pearadmin.system.service.ISysRoleService;
import com.pearadmin.system.service.ISysUserRoleService;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("system/role")
public class SysRoleController extends BaseController {

    private String MODULE_PATH = "system/role/";

    @Resource
    private ISysRoleService sysRoleService;
    @Resource
    private ISysUserRoleService sysUserRoleService;

    @GetMapping("main")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH + "main");
        return modelAndView;
    }

    @PostMapping("data")
    public TableVO data(SysRole sysRole,Integer page,Integer limit){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();

        if(sysRole.getName()!=null && sysRole.getName()!=""){
            queryWrapper.eq("name",sysRole.getName());
        }

        if(sysRole.getCode()!=null && sysRole.getCode()!=""){
            queryWrapper.eq("code",sysRole.getCode());
        }


        IPage<SysRole> pageInfo = sysRoleService.page(new Page<>(page,limit),queryWrapper);
        return PageTable(pageInfo.getRecords(),pageInfo.getTotal());
    }

    @GetMapping("add")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"add");
        return modelAndView;
    }

    @GetMapping("power")
    public ModelAndView power(@RequestParam String id,ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"power");
        modelAndView.addObject("roleId",id);
        return modelAndView;
    }

    @PostMapping("save")
    public Result save(@RequestBody SysRole sysRole){
        System.out.println(sysRole.toString());

        boolean result =  sysRoleService.save(sysRole);
        return choice(result);
    }

    @GetMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView,String id){
        SysRole sysRole = sysRoleService.getById(id);
        modelAndView.addObject("sysRole",sysRole);
        modelAndView.setViewName(MODULE_PATH+"edit");
        return modelAndView;
    }

    @PutMapping("update")
    public Result update(SysRole sysRole){
        Boolean result = sysRoleService.updateById(sysRole);
        return choice(result);
    }

    @PutMapping("enable/{id}")
    public Result enable(@PathVariable String id){

        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setStatus("0");

        boolean result = sysRoleService.updateById(sysRole);
        return choice(result);
    }

    @PutMapping("disable/{id}")
    public Result disable(@PathVariable String id){
        System.out.println("后台编号:"+id);
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setStatus("1");

        boolean result = sysRoleService.updateById(sysRole);
        return choice(result);
    }


    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable("id")String id){
        boolean result = sysRoleService.removeById(id);
        //判定该角色是否绑定用户
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        if(sysUserRoleService.list(queryWrapper).size()>0){
              return warming("该角色已绑定用户,无法删除");
        }
        return choice(result);
    }

    @DeleteMapping("deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable("ids")String ids){
       List<String> arrs = Arrays.asList(ids.split(","));
       boolean result = sysRoleService.removeByIds(arrs);
       return choice(result);
    }


}
